package org.i_plus.model.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.i_plus.model.FriendBean;
import org.i_plus.model.GiftBean;
import org.i_plus.model.Gift_RcdBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Gift_RcdDAOHibernate {
	private SessionFactory sessionFactory;
	public void setSessionFactory(SessionFactory sessionFactory)
	{
		this.sessionFactory=sessionFactory;
	}
	public void selectAll()
	{
		Session ses=this.sessionFactory.getCurrentSession();
		Query query=ses.createQuery("from Gift_RcdBean");
		List<Gift_RcdBean> list=query.list();
	}
	public void select_By_num(int num)
	{
		Session ses=this.sessionFactory.getCurrentSession();
		Gift_RcdBean bean=(Gift_RcdBean)ses.get(Gift_RcdBean.class, num);
	}
	public void insert(String toacc,String fromacc,java.sql.Timestamp time,int giftid,int gcount)
	{
		Session ses=this.sessionFactory.getCurrentSession();
		Gift_RcdBean bean=new Gift_RcdBean();
		bean.setToacc(toacc);
		bean.setFromacc(fromacc);
		bean.setTime(time);
		GiftBean gift = new GiftBean();
		gift.setGiftid(giftid);
		bean.setGiftbean(gift);
		bean.setGcount(gcount);
		ses.save(bean);
	}
	public void update(String toacc,String fromacc,java.sql.Timestamp time,int giftid,int gcount,int num)
	{
		Session ses=this.sessionFactory.getCurrentSession();
		Gift_RcdBean bean=(Gift_RcdBean)ses.get(Gift_RcdBean.class, num);
		bean.setToacc(toacc);
		bean.setFromacc(fromacc);
		bean.setTime(time);
		GiftBean gift = new GiftBean();
		gift.setGiftid(giftid);
		bean.setGiftbean(gift);
		bean.setGcount(gcount);
		ses.saveOrUpdate(bean);
	}
	public void delete(int num)
	{
		Session ses=this.sessionFactory.getCurrentSession();
		Query query=ses.createQuery("delete from Gift_RcdBean where num=?");
		query.setParameter(0, num);
		query.executeUpdate();
	}
	public List<Gift_RcdBean> selectByAccount(String account)
	{
		Session ses=this.sessionFactory.getCurrentSession();
		Query query=ses.createQuery(" from Gift_RcdBean where fromacc=?");
		query.setString(0, account);
		return query.list();
	}
	
	
	public static void main(String[] args) {
		ApplicationContext context=new ClassPathXmlApplicationContext("beans.config.xml");
		Gift_RcdDAOHibernate mh=(Gift_RcdDAOHibernate)context.getBean("Gift_RcdDAO");
		mh.sessionFactory.getCurrentSession().beginTransaction();
//		SimpleDateFormat sf=new SimpleDateFormat("dd-MM-yyyy");
//		java.sql.Date time=java.sql.Date.valueOf("1985-5-25");
		//mh.update("bird", "dog", time, 3, 2, 3);
		//mh.selectAll();
		//mh.delete(3);
//		for(Gift_RcdBean temp:mh.selectByAccount("a19897811@yahoo.com"))
//			System.out.println(temp.getFromacc()+":"+temp.getToacc()+":"+temp.getNum());

		
		//mh.select_By_num(2);
		mh.sessionFactory.getCurrentSession().getTransaction().commit();
		
	}

}
